/* eslint-disable no-restricted-globals */
/* eslint-disable no-shadow */
/* eslint-disable no-unused-vars */
import { defineStore } from 'pinia';

import useAuthStore from './auth.store';

export enum ModeEnum {
  DEV = 'DEV',
  PROD = 'PROD',
}

interface State {
  env: string;
  nodeEnv: string;
  current: ModeEnum;
  account: string;
}
const getCurrent = () => {
  if (import.meta.env.MODE === 'development' || location.hostname.includes('dev')) {
    return localStorage.getItem('mode') ? localStorage.getItem('mode') : ModeEnum.DEV;
  }
  return ModeEnum.PROD;
};

let env = 'prod';
if (location.hostname.startsWith('local')) env = 'dev';
if (location.hostname.startsWith('dev')) env = 'dev';

const useModeStore = defineStore('mode', {
  state: () => ({
    env,
    nodeEnv:
        import.meta.env.MODE === 'development' || location.hostname.includes('dev')
          ? 'development'
          : 'production',
    current: getCurrent(),
    account: useAuthStore().account,
  }) as State,
  actions: {
    setMode(e: ModeEnum) {
      localStorage.setItem('mode', e);
      this.current = e;
    },
  },
});

export default useModeStore;
